How to Collect Data Using Twitter API V2 For Natural Language Processing

How to Collect Data Using Twitter API V2 For Natural Language Processing

In this blog, we will use twitter developer account to collect data from Twitter API V2. We will collect some recent tweets and convert into pandas dataframe. We will also collect recent tweets for particular twitter user.

Sign up to twitter developer platform

https://developer.twitter.com/en

We need twitter developer account. If you have twitter account you can login to https://developer.twitter.com/en.

I don't have twitter account, so i am creating.

Click on Sign up

Click on Sign up.

We can use Sign up with Google/Sign up with Apple or Sign up with phone or email. I am using Sign up with phone or email.

Enter your Name, email and date of birth then click on Next.

Click on Next

Click on Sign up.

Verification code will come to your email. Enter verification code then click on Next.

You have to enter password then click Next.

It will ask you to upload profile picture. If you want you can upload or you can skip for now.

Same for bio, if you want to write, then write or otherwise skip for now.

Now twitter account created.

Go to this url: https://developer.twitter.com/en -> then click on Developer Portal

Verify twitter account

As this twitter account is new, so it will not verify now. I have to wait for atleast one week to verify. After verification create project and app.

Create Project and App in developer twitter account

After verification, go to https://developer.twitter.com/ and create project and app. I have created project1 and my app name is xv-app.

When we create app, we will get keys and tokens like below. We have to copy the keys and tokens, later we need that.

Install Tweepy Library

An easy-to-use Python library for accessing the Twitter API.

Installation

The easiest way to install the latest version from PyPI is by using pip:

pip install tweepy

Import library

In [3]:
import tweepy
In [4]:
tweepy.__version__
Out[4]:
'4.10.0'

In my system, latest version of tweepy is 4.10.0. If you have already installed, you can upgrade tweepy by using this pip command.

pip install --upgrade tweepy

View available methods in tweepy module

In [5]:
dir(tweepy)
Out[5]:
['API',
 'AppAuthHandler',
 'BadRequest',
 'Cache',
 'Client',
 'Cursor',
 'FileCache',
 'Forbidden',
 'HTTPException',
 'List',
 'Media',
 'MemoryCache',
 'NotFound',
 'OAuth1UserHandler',
 'OAuth2AppHandler',
 'OAuth2BearerHandler',
 'OAuth2UserHandler',
 'OAuthHandler',
 'Paginator',
 'Place',
 'Poll',
 'ReferencedTweet',
 'Response',
 'Space',
 'Stream',
 'StreamResponse',
 'StreamRule',
 'StreamingClient',
 'TooManyRequests',
 'TweepyException',
 'Tweet',
 'TwitterServerError',
 'Unauthorized',
 'User',
 '__author__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__license__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'api',
 'auth',
 'cache',
 'client',
 'cursor',
 'errors',
 'list',
 'media',
 'mixins',
 'models',
 'pagination',
 'parsers',
 'place',
 'poll',
 'space',
 'streaming',
 'tweet',
 'user',
 'utils']

help(tweepy.Client)

class Client(BaseClient)

 |  Client(bearer_token=None, consumer_key=None, consumer_secret=None, access_token=None, access_token_secret=None, *, return_type=<class 'tweepy.client.Response'>, wait_on_rate_limit=False)
 |  
 |  Client(         bearer_token=None, consumer_key=None, consumer_secret=None,         access_token=None, access_token_secret=None, *, return_type=Response,         wait_on_rate_limit=False     )
 |  
 |  Twitter API v2 Client
 |  
 |  .. versionadded:: 4.0
 |  
 |  Parameters
 |  ----------
 |  bearer_token : str | None
 |      Twitter API OAuth 2.0 Bearer Token / Access Token
 |  consumer_key : str | None
 |      Twitter API OAuth 1.0a Consumer Key
 |  consumer_secret : str | None
 |      Twitter API OAuth 1.0a Consumer Secret
 |  access_token : str | None
 |      Twitter API OAuth 1.0a Access Token
 |  access_token_secret : str | None
 |      Twitter API OAuth 1.0a Access Token Secret
 |  return_type : type[dict | requests.Response | Response]
 |      Type to return from requests to the API
 |  wait_on_rate_limit : bool
 |      Whether to wait when rate limit is reached
 |  
 |  Attributes
 |  ----------
 |  session : requests.Session
 |      Requests Session used to make requests to the API
 |  user_agent : str
 |      User agent used when making requests to the API
 |  
 |  Method resolution order:
 |      Client
 |      BaseClient
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  add_list_member(self, id, user_id, *, user_auth=True)
 |      Enables the authenticated user to add a member to a List they own.
 |      
 |      .. versionadded:: 4.2
 |      
 |      .. versionchanged:: 4.5
 |          Added ``user_auth`` parameter
 |      
 |      Parameters
 |      ----------
 |      id : int | str
 |          The ID of the List you are adding a member to.
 |      user_id : int | str
 |          The ID of the user you wish to add as a member of the List.
 |      user_auth : bool
 |          Whether or not to use OAuth 1.0a User Context to authenticate
 |      
 |      Returns
 |      -------
 |      dict | requests.Response | Response
 |      

Declare bearer_token of twitter api

In [7]:
bearer_token = 'AAAAAAAAAAAAAAAAAAAAANvYfwEAAAAAD6QJD%2FFEorvUIwOusfma61Vzxe8%3DVcIrTho7pJBqmuvh95U1LWyKK7T9NclTGPyj137auvIF0VuUPs'
In [8]:
client = tweepy.Client(bearer_token = bearer_token)
client
Out[8]:
<tweepy.client.Client at 0x7f7d396b2f10>

We can see lot of methods are available with client using dir() method.

In [9]:
dir(client)
Out[9]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_get_authenticating_user_id',
 '_get_oauth_1_authenticating_user_id',
 '_get_oauth_2_authenticating_user_id',
 '_make_request',
 '_process_data',
 '_process_includes',
 '_process_params',
 'access_token',
 'access_token_secret',
 'add_list_member',
 'bearer_token',
 'block',
 'bookmark',
 'consumer_key',
 'consumer_secret',
 'create_compliance_job',
 'create_list',
 'create_tweet',
 'delete_list',
 'delete_tweet',
 'follow',
 'follow_list',
 'follow_user',
 'get_all_tweets_count',
 'get_blocked',
 'get_bookmarks',
 'get_compliance_job',
 'get_compliance_jobs',
 'get_followed_lists',
 'get_home_timeline',
 'get_liked_tweets',
 'get_liking_users',
 'get_list',
 'get_list_followers',
 'get_list_members',
 'get_list_memberships',
 'get_list_tweets',
 'get_me',
 'get_muted',
 'get_owned_lists',
 'get_pinned_lists',
 'get_quote_tweets',
 'get_recent_tweets_count',
 'get_retweeters',
 'get_space',
 'get_space_buyers',
 'get_space_tweets',
 'get_spaces',
 'get_tweet',
 'get_tweets',
 'get_user',
 'get_users',
 'get_users_followers',
 'get_users_following',
 'get_users_mentions',
 'get_users_tweets',
 'hide_reply',
 'like',
 'mute',
 'pin_list',
 'remove_bookmark',
 'remove_list_member',
 'request',
 'return_type',
 'retweet',
 'search_all_tweets',
 'search_recent_tweets',
 'search_spaces',
 'session',
 'unblock',
 'unfollow',
 'unfollow_list',
 'unfollow_user',
 'unhide_reply',
 'unlike',
 'unmute',
 'unpin_list',
 'unretweet',
 'update_list',
 'user_agent',
 'wait_on_rate_limit']

Search recent tweets

search_recent_tweets(query, *, user_auth=False, **params) method of tweepy.client.Client instance

search_recent_tweets(query, *, end_time=None, expansions=None, max_results=None, media_fields=None, next_token=None, place_fields=None, poll_fields=None, since_id=None, sort_order=None, start_time=None, tweet_fields=None, until_id=None,    user_fields=None, user_auth=False)

The recent search endpoint returns Tweets from the last seven days that *match a search query.

The Tweets returned by this endpoint count towards the Project-level
`Tweet cap`_.

.. versionchanged:: 4.6
    Added ``sort_order`` parameter

Parameters
----------
query : str
    One rule for matching Tweets. If you are using a
    `Standard Project`_ at the Basic `access level`_, you can use the
    basic set of `operators`_ and can make queries up to 512 characters
    long. If you are using an `Academic Research Project`_ at the Basic
    access level, you can use all available operators and can make
    queries up to 1,024 characters long.

end_time : datetime.datetime | str | None
    YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). The newest, most recent
    UTC timestamp to which the Tweets will be provided. Timestamp is in
    second granularity and is exclusive (for example, 12:00:01 excludes
    the first second of the minute). By default, a request will return
    Tweets from as recent as 30 seconds ago if you do not include this
    parameter.

expansions : list[str] | str | None
    :ref:`expansions_parameter`

max_results : int | None
    The maximum number of search results to be returned by a request. A
    number between 10 and 100. By default, a request response will
    return 10 results.

media_fields : list[str] | str | None
    :ref:`media_fields_parameter`

next_token : str | None
    This parameter is used to get the next 'page' of results. The value
    used with the parameter is pulled directly from the response
    provided by the API, and should not be modified.

place_fields : list[str] | str | None
    :ref:`place_fields_parameter`

poll_fields : list[str] | str | None
    :ref:`poll_fields_parameter`

since_id : int | str | None
    Returns results with a Tweet ID greater than (that is, more recent
    than) the specified ID. The ID specified is exclusive and responses
    will not include it. If included with the same request as a
    ``start_time`` parameter, only ``since_id`` will be used.

sort_order : str | None
    This parameter is used to specify the order in which you want the
    Tweets returned. By default, a request will return the most recent
    Tweets first (sorted by recency).

start_time : datetime.datetime | str | None
    YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). The oldest UTC timestamp
    (from most recent seven days) from which the Tweets will be
    provided. Timestamp is in second granularity and is inclusive (for
    example, 12:00:01 includes the first second of the minute). If
    included with the same request as a ``since_id`` parameter, only
    ``since_id`` will be used. By default, a request will return Tweets
    from up to seven days ago if you do not include this parameter.

tweet_fields : list[str] | str | None
    :ref:`tweet_fields_parameter`

until_id : int | str | None
    Returns results with a Tweet ID less than (that is, older than) the
    specified ID. The ID specified is exclusive and responses will not
    include it.

user_fields : list[str] | str | None
    :ref:`user_fields_parameter`

user_auth : bool
    Whether or not to use OAuth 1.0a User Context to authenticate

Returns
-------
dict | requests.Response | Response
In [10]:
query = "datascience"

Searching last one week 10 tweets

In [12]:
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'], max_results=10)

tweets
Out[12]:
Response(data=[<Tweet id=1559949207816916992 text='RT What I’ve Learned After Using Git Daily for 6 Months https://t.co/E1NtME4uy4 #versioncontrol #programming #git #dataanalytics #datascience https://t.co/PXJ7W14ooZ'>, <Tweet id=1559949206877618181 text='RT @Nostalgicbrain0: Contrast between #DeepLearning and #MachineLearning #DataScience #SQL #Cybersecurity #BigData #Analytics #AI #IIoT #Py…'>, <Tweet id=1559949195301322753 text='RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…'>, <Tweet id=1559949195238424576 text='RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :\n#DataScience #MachineLearning  #Analytics #AI #M…'>, <Tweet id=1559949195225796609 text='RT @rasangarocks: Algorithms (4th Edition)\n\nLink - https://t.co/Sngm6FnmF0\n\n#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…'>, <Tweet id=1559949195221635072 text='RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…'>, <Tweet id=1559949195221630976 text='RT @rasangarocks: Algorithms (4th Edition)\n\nLink - https://t.co/Sngm6FnmF0\n\n#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…'>, <Tweet id=1559949195217453056 text='RT @KirkDBorne: 42 Best Resources to Learn #Python for Beginners (YouTube, Courses, Books, &amp; Tutorials) 2022 — compiled by @tut_ml \n————\n#C…'>, <Tweet id=1559949195200663554 text='RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :\n#DataScience #MachineLearning  #Analytics #AI #M…'>, <Tweet id=1559949176577851395 text='RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :\n#DataScience #MachineLearning  #Analytics #AI #M…'>], includes={}, errors=[], meta={'newest_id': '1559949207816916992', 'oldest_id': '1559949176577851395', 'result_count': 10, 'next_token': 'b26v89c19zqg8o3fpz5nx03siud17kktfim9pixhm2p31'})
In [13]:
for tweet in tweets.data:
    print("Text: ", tweet.text)
    print("Created at: ", tweet.created_at)
    print("\n")
Text:  RT What I’ve Learned After Using Git Daily for 6 Months https://t.co/E1NtME4uy4 #versioncontrol #programming #git #dataanalytics #datascience https://t.co/PXJ7W14ooZ
Created at:  2022-08-17 17:04:08+00:00


Text:  RT @Nostalgicbrain0: Contrast between #DeepLearning and #MachineLearning #DataScience #SQL #Cybersecurity #BigData #Analytics #AI #IIoT #Py…
Created at:  2022-08-17 17:04:08+00:00


Text:  RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @rasangarocks: Algorithms (4th Edition)

Link - https://t.co/Sngm6FnmF0

#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @rasangarocks: Algorithms (4th Edition)

Link - https://t.co/Sngm6FnmF0

#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @KirkDBorne: 42 Best Resources to Learn #Python for Beginners (YouTube, Courses, Books, &amp; Tutorials) 2022 — compiled by @tut_ml 
————
#C…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…
Created at:  2022-08-17 17:04:05+00:00


Text:  RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…
Created at:  2022-08-17 17:04:00+00:00


Annotations have been added to the Tweet object from all v2 endpoints that return a Tweet object. Tweet annotations offer a way to understand contextual information about the Tweet itself. Though 100% of Tweets are reviewed, due to the contents of Tweet text, only a portion are annotated.

1. Entity annotations (NER): Entities are comprised of people, places, products, and organizations. Entities are delivered as part of the entity payload section. They are programmatically assigned based on what is explicitly mentioned (named-entity recognition) in the Tweet text.

2. Context annotations: Derived from the analysis of a Tweet’s text and will include a domain and entity pairing which can be used to discover Tweets on topics that may have been previously difficult to surface. At present, we’re using a list of 80+ domains to categorize Tweets.

Tweet annotation types

  1. Entities

Entity annotations are programmatically defined entities that are nested within the entities field and are reflected as annotations in the payload. Each annotation has a confidence score and an indication of where in the Tweet text the entities were identified (start and end fields).

The entity annotations can have the following types:

Person - Barack Obama, Daniel, or George W. Bush

Place - Detroit, Cali, or "San Francisco, California"

Product - Mountain Dew, Mozilla Firefox

Organization - Chicago White Sox, IBM

Other - Diabetes, Super Bowl 50

  1. Context

Context annotations are delivered as a context_annotations field in the payload. These annotations are inferred based on semantic analysis (keywords, hashtags, handles, etc) of the Tweet text and result in domain and/or entity labels. Context annotations can yield one or many domains.

In [14]:
for tweet in tweets.data:
    print(tweet.text)
    if len(tweet.context_annotations) > 0:
        print("\n")
        print(tweet.context_annotations)
RT What I’ve Learned After Using Git Daily for 6 Months https://t.co/E1NtME4uy4 #versioncontrol #programming #git #dataanalytics #datascience https://t.co/PXJ7W14ooZ


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}]
RT @Nostalgicbrain0: Contrast between #DeepLearning and #MachineLearning #DataScience #SQL #Cybersecurity #BigData #Analytics #AI #IIoT #Py…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '898650876658634752', 'name': 'Cybersecurity', 'description': 'Cybersecurity'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '1047123725525479425', 'name': 'Information security', 'description': 'Information Security'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '1118167720866930689', 'name': 'Big Data'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '847878884917886977', 'name': 'Politics', 'description': 'Politics'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '898650876658634752', 'name': 'Cybersecurity', 'description': 'Cybersecurity'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '900740740468191232', 'name': 'Political issues', 'description': 'Political issues'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '1047123725525479425', 'name': 'Information security', 'description': 'Information Security'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '1088443224425807872', 'name': 'Information Privacy Worldwide', 'description': 'Information Privacy'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}]
RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '1118167720866930689', 'name': 'Big Data'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '1357506193879486469', 'name': 'Python'}}, {'domain': {'id': '165', 'name': 'Technology', 'description': 'for individual and types of technology, e.g., food technology, 3D printing'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}]
RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}]
RT @rasangarocks: Algorithms (4th Edition)

Link - https://t.co/Sngm6FnmF0

#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…


[{'domain': {'id': '45', 'name': 'Brand Vertical', 'description': 'Top level entities that describe a Brands industry'}, 'entity': {'id': '781974596157251587', 'name': 'Government/Education'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '781974597226729473', 'name': 'Non-profit - Government/Education'}}, {'domain': {'id': '47', 'name': 'Brand', 'description': 'Brands and Companies'}, 'entity': {'id': '10043701943', 'name': 'Women in tech'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '10043701943', 'name': 'Women in tech'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '45', 'name': 'Brand Vertical', 'description': 'Top level entities that describe a Brands industry'}, 'entity': {'id': '781974596157251587', 'name': 'Government/Education'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '781974596165570560', 'name': 'Education Related - Government/Education'}}, {'domain': {'id': '47', 'name': 'Brand', 'description': 'Brands and Companies'}, 'entity': {'id': '10043701955', 'name': 'Women Who Code'}}]
RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '1118167720866930689', 'name': 'Big Data'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '1357506193879486469', 'name': 'Python'}}, {'domain': {'id': '165', 'name': 'Technology', 'description': 'for individual and types of technology, e.g., food technology, 3D printing'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '898657691010191360', 'name': 'Internet of things', 'description': 'Internet of Things'}}]
RT @rasangarocks: Algorithms (4th Edition)

Link - https://t.co/Sngm6FnmF0

#Algorithms #Maths #100DaysOfCode #CodeNewbies #WomenWhoCode #D…


[{'domain': {'id': '45', 'name': 'Brand Vertical', 'description': 'Top level entities that describe a Brands industry'}, 'entity': {'id': '781974596157251587', 'name': 'Government/Education'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '781974597226729473', 'name': 'Non-profit - Government/Education'}}, {'domain': {'id': '47', 'name': 'Brand', 'description': 'Brands and Companies'}, 'entity': {'id': '10043701943', 'name': 'Women in tech'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '10043701943', 'name': 'Women in tech'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '45', 'name': 'Brand Vertical', 'description': 'Top level entities that describe a Brands industry'}, 'entity': {'id': '781974596157251587', 'name': 'Government/Education'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '781974596165570560', 'name': 'Education Related - Government/Education'}}, {'domain': {'id': '47', 'name': 'Brand', 'description': 'Brands and Companies'}, 'entity': {'id': '10043701955', 'name': 'Women Who Code'}}]
RT @KirkDBorne: 42 Best Resources to Learn #Python for Beginners (YouTube, Courses, Books, &amp; Tutorials) 2022 — compiled by @tut_ml 
————
#C…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '66', 'name': 'Interests and Hobbies Category', 'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '848921413196984320', 'name': 'Computer programming', 'description': 'Computer programming'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '1196446161223028736', 'name': 'Social media'}}, {'domain': {'id': '165', 'name': 'Technology', 'description': 'for individual and types of technology, e.g., food technology, 3D printing'}, 'entity': {'id': '1196446161223028736', 'name': 'Social media'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '781974596752842752', 'name': 'Services'}}, {'domain': {'id': '47', 'name': 'Brand', 'description': 'Brands and Companies'}, 'entity': {'id': '10029382357', 'name': 'YouTube'}}, {'domain': {'id': '131', 'name': 'Unified Twitter Taxonomy', 'description': 'A taxonomy view into the Semantic Core knowledge graph'}, 'entity': {'id': '10029382357', 'name': 'YouTube'}}]
RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}]
RT @Manish_kumar3_1: 10 websites so good that you will regret not knowing them ( I bet ) :
#DataScience #MachineLearning  #Analytics #AI #M…


[{'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '848920371311001600', 'name': 'Technology', 'description': 'Technology and computing'}}, {'domain': {'id': '30', 'name': 'Entities [Entity Service]', 'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'}, 'entity': {'id': '930484568305541120', 'name': 'Data science', 'description': 'Data Science'}}]

Get tweets and convert into pandas dataframe

In [16]:
import requests

Create Client

In this time, we are passing return_type: type to return from requests to the API.

In [18]:
client1 = tweepy.Client(bearer_token = bearer_token, return_type = requests.Response)
client1
Out[18]:
<tweepy.client.Client at 0x7f7d1f5b2d10>

Get recent tweets

In [19]:
new_tweets = client1.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'], max_results=10)
new_tweets
Out[19]:
<Response [200]>

Convert tweets into json

In [20]:
tweets_json = new_tweets.json() 
tweets_json
Out[20]:
{'data': [{'id': '1559950757943775239',
   'created_at': '2022-08-17T17:10:17.000Z',
   'text': 'Check out a detailed #DataScience cheat-sheet that covers #MachineLearning, probability, statistics, and much more useful information:🧠\n\n👉https://t.co/RwSHDuKsCU👈\n\nby @MIT_CSAIL @MaverickLin @KirkDBorne\n#ML #AI #AINews #DataScientists #CodeNewbies #Python #Coding #Programming https://t.co/beuh37qNuC',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '848921413196984320',
      'name': 'Computer programming',
      'description': 'Computer programming'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848921413196984320',
      'name': 'Computer programming',
      'description': 'Computer programming'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '1291344241956855808',
      'name': 'Massachusetts Institute of Technology'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '1357506193879486469', 'name': 'Python'}},
    {'domain': {'id': '157',
      'name': 'Colleges & Universities',
      'description': 'Colleges & universities around the world'},
     'entity': {'id': '1291344241956855808',
      'name': 'Massachusetts Institute of Technology'}},
    {'domain': {'id': '165',
      'name': 'Technology',
      'description': 'for individual and types of technology, e.g., food technology, 3D printing'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}}]},
  {'id': '1559950728256319491',
   'created_at': '2022-08-17T17:10:10.000Z',
   'text': 'RT @AsifAli02470010: Array sorting Algorithms\n\n#ArtificialIntelligence #AI #ML #DataScience #DataScientists #innovation #CodeNewbies #Tech…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}}]},
  {'id': '1559950712414429184',
   'created_at': '2022-08-17T17:10:06.000Z',
   'text': 'RT @gp_pulipaka: #DataTypes in Python You Need to Know. #BigData #Analytics #DataScience #AI #MachineLearning #IoT #IIoT #Python #RStats #T…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '848921413196984320',
      'name': 'Computer programming',
      'description': 'Computer programming'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '1118167720866930689', 'name': 'Big Data'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848921413196984320',
      'name': 'Computer programming',
      'description': 'Computer programming'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '1357506193879486469', 'name': 'Python'}},
    {'domain': {'id': '165',
      'name': 'Technology',
      'description': 'for individual and types of technology, e.g., food technology, 3D printing'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}}]},
  {'id': '1559950680739266563',
   'created_at': '2022-08-17T17:09:59.000Z',
   'text': 'RT @Ronald_vanLoon: What is hyperconvergence?\nby @jfruh @NetworkWorld\n\nRead more: https://t.co/c8CcmI6d7d\n\n#IoT #BigData #InternetofThings…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '1118167720866930689', 'name': 'Big Data'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}}]},
  {'id': '1559950663441850370',
   'created_at': '2022-08-17T17:09:55.000Z',
   'text': 'RT @Ronald_vanLoon: What is hyperconvergence?\nby @jfruh @NetworkWorld\n\nRead more: https://t.co/c8CcmI6d7d\n\n#IoT #BigData #InternetofThings…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '1118167720866930689', 'name': 'Big Data'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '898657691010191360',
      'name': 'Internet of things',
      'description': 'Internet of Things'}}]},
  {'id': '1559950659880976386',
   'created_at': '2022-08-17T17:09:54.000Z',
   'text': 'How CoRise Helped Ben Wilson Land a New Job as a Analytics Engineer (and a Side Gig in Doodling) via/kdnuggets #datascience #AI #ML #data #BigData https://t.co/HgAZVFId1p',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '1118167720866930689', 'name': 'Big Data'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}}]},
  {'id': '1559950621641687040',
   'created_at': '2022-08-17T17:09:45.000Z',
   'text': 'RT @MikeTamir: You Should Use This to Visualize SQL Joins Instead of Venn Diagrams https://t.co/L0IY6TvPjC\n#AI #DataScience #DeepLearning #…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}}]},
  {'id': '1559950548723695616',
   'created_at': '2022-08-17T17:09:27.000Z',
   'text': 'RT @rasangarocks: Hands-On Python BEGINNER: with 162 Exercises, 3 Projects, 3 Assignments &amp; Final Exam\n\nLink - https://t.co/2IhPY2UrdW\n\n#Py…'},
  {'id': '1559950433011081217',
   'created_at': '2022-08-17T17:09:00.000Z',
   'text': 'RT @machinelearnflx: IBM AI Engineering course https://t.co/9Zd8Skgc8F #machinelearning #datascience #datascientist #datascientist #DataAna…',
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}}]},
  {'id': '1559950296712822784',
   'created_at': '2022-08-17T17:08:27.000Z',
   'text': "UO's COE is hiring an endowed professor of educational data science (associate or full professor rank) to teach in our quantitative research methods in education program. File review begins Oct 2! #datascience #hiring #DataScientist #rstats #RLadies\nhttps://t.co/5k9oGLSn2a",
   'context_annotations': [{'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '781974596752842752', 'name': 'Services'}},
    {'domain': {'id': '48',
      'name': 'Product',
      'description': 'Products created by Brands.  Examples: Ford Explorer, Apple iPhone.'},
     'entity': {'id': '1412579054855671809', 'name': 'Google Innovation'}},
    {'domain': {'id': '65',
      'name': 'Interests and Hobbies Vertical',
      'description': 'Top level interests and hobbies groupings, like Food or Travel'},
     'entity': {'id': '847544972781826048',
      'name': 'Careers',
      'description': 'Careers'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '848920371311001600',
      'name': 'Technology',
      'description': 'Technology and computing'}},
    {'domain': {'id': '30',
      'name': 'Entities [Entity Service]',
      'description': 'Entity Service top level domain, every item that is in Entity Service should be in this domain'},
     'entity': {'id': '930484568305541120',
      'name': 'Data science',
      'description': 'Data Science'}},
    {'domain': {'id': '66',
      'name': 'Interests and Hobbies Category',
      'description': 'A grouping of interests and hobbies entities, like Novelty Food or Destinations'},
     'entity': {'id': '961961812492148736',
      'name': 'Recruitment',
      'description': 'Recruitment'}},
    {'domain': {'id': '131',
      'name': 'Unified Twitter Taxonomy',
      'description': 'A taxonomy view into the Semantic Core knowledge graph'},
     'entity': {'id': '854692455005921281',
      'name': 'Science',
      'description': 'Science'}}]}],
 'meta': {'newest_id': '1559950757943775239',
  'oldest_id': '1559950296712822784',
  'result_count': 10,
  'next_token': 'b26v89c19zqg8o3fpz5nx042xmck6ldwrg193594qixkt'}}

Convert into pandas dataframe

In [21]:
import pandas as pd
In [22]:
tweets_data = tweets_json['data'] 
df = pd.json_normalize(tweets_data) 
df
Out[22]:
id created_at text context_annotations
0 1559950757943775239 2022-08-17T17:10:17.000Z Check out a detailed #DataScience cheat-sheet ... [{'domain': {'id': '30', 'name': 'Entities [En...
1 1559950728256319491 2022-08-17T17:10:10.000Z RT @AsifAli02470010: Array sorting Algorithms\... [{'domain': {'id': '30', 'name': 'Entities [En...
2 1559950712414429184 2022-08-17T17:10:06.000Z RT @gp_pulipaka: #DataTypes in Python You Need... [{'domain': {'id': '30', 'name': 'Entities [En...
3 1559950680739266563 2022-08-17T17:09:59.000Z RT @Ronald_vanLoon: What is hyperconvergence?\... [{'domain': {'id': '30', 'name': 'Entities [En...
4 1559950663441850370 2022-08-17T17:09:55.000Z RT @Ronald_vanLoon: What is hyperconvergence?\... [{'domain': {'id': '30', 'name': 'Entities [En...
5 1559950659880976386 2022-08-17T17:09:54.000Z How CoRise Helped Ben Wilson Land a New Job as... [{'domain': {'id': '30', 'name': 'Entities [En...
6 1559950621641687040 2022-08-17T17:09:45.000Z RT @MikeTamir: You Should Use This to Visualiz... [{'domain': {'id': '30', 'name': 'Entities [En...
7 1559950548723695616 2022-08-17T17:09:27.000Z RT @rasangarocks: Hands-On Python BEGINNER: wi... NaN
8 1559950433011081217 2022-08-17T17:09:00.000Z RT @machinelearnflx: IBM AI Engineering course... [{'domain': {'id': '30', 'name': 'Entities [En...
9 1559950296712822784 2022-08-17T17:08:27.000Z UO's COE is hiring an endowed professor of edu... [{'domain': {'id': '30', 'name': 'Entities [En...

We converted recent tweets in pandas dataframe. Now we have data, whatever we want, we can do with data.

In [20]:
df['text']
Out[20]:
0    RT @PDH_Metaverse: Breaking #Crypto News \n\nL...
1    RT @Analytics_699: 10 Key Types of Data Analys...
2    RT @PinakiLaskar: An Impartial Take to the CNN...
3    2022-08-16 18:46:17.077162 : #BNBDOWNUSDT is p...
4    RT @Analytics_699: 10 Key Types of Data Analys...
5    @Jose_Gilartete This thread is saved to your N...
6    RT @Sheilaessays: Hey!!\nFall Classes begin to...
7    RT @Sheilaessays: Our writers are competent an...
8    RT @24_EssayDue: Our writers are competent and...
9    RT @rasangarocks: Real-World Python: A Hacker'...
Name: text, dtype: object

Collect tweets for particular twitter user

Suppose we want to collect data for particular user. In this example, i want to get Rahul Gandhi's tweets. We need Rahul Gandhi's twitter id to fetch the tweets. To get twitter id, i used this url https://tweeterid.com/ and pass the twitter user name and got the twitter id of Rahul Gandhi's account.

In [29]:
id = '3171712086'

tweets = client.get_users_tweets(id=id, tweet_fields=['context_annotations','created_at','geo'])

for tweet in tweets.data:
    print("\nTweet text: ", tweet.text)
    print("Created at: ", tweet.created_at)
Tweet text:  5 महीने की गर्भवती महिला से बलात्कार और उनकी 3 साल की बच्ची की हत्या करने वालों को 'आज़ादी के अमृत महोत्सव' के दौरान रिहा किया गया।

नारी शक्ति की झूठी बातें करने वाले देश की महिलाओं को क्या संदेश दे रहे हैं?

प्रधानमंत्री जी, पूरा देश आपकी कथनी और करनी में अंतर देख रहा है।
Created at:  2022-08-17 05:16:39+00:00

Tweet text:  "Many diversities that have defined us for centuries are being used to divide us...Nationalism, instead of building pride, is being used to propagate prejudice."

Congress President Smt. Sonia Gandhi sheds light upon the dichotomies being created by this authoritarian regime. https://t.co/R90sqsGFbb
Created at:  2022-08-16 10:50:00+00:00

Tweet text:  जम्मू-कश्मीर के पहलगाम इलाके में आईटीबीपी के 39 जवानों से भरी बस के खाई में गिरने की ख़बर बेहद दुःखद है।  

मैं घायल जवानों के जल्द स्वस्थ होने और शहीद जवानों की आत्मा की शांति की कामना करता हूं एवं शोक-संतप्त परिवारों के प्रति अपनी गहरी संवेदनाएं प्रकट करता हूं।
Created at:  2022-08-16 07:45:48+00:00

Tweet text:  कांग्रेस परिवार के सदस्यों ने पूरे जोश से 9-15 अगस्त तक देश में तिरंगे के साथ सफलतापूर्वक 'आज़ादी गौरव यात्रा' निकाली। 

कांग्रेस के सभी कार्यकर्ताओं और नेताओं को बधाई, खासतौर पर बेंगलुरु के कांग्रेस परिवार को, जहां जनसैलाब उमड़ा। 

हम साथ मिलकर देश के हर सपने को पूरा करेंगे। https://t.co/F1I3k3c6du
Created at:  2022-08-16 05:10:11+00:00

Tweet text:  आज़ादी के 75 साल पूरे होने पर आज कांग्रेस मुख्यालय में ध्वजारोहण समारोह में शामिल हुआ, ये एक ऐतिहासिक और यादगार पल है।  

आज हम आज़ादी के 76वें वर्ष में प्रवेश कर रहे हैं, हमें मिलकर एक नई ऊर्जा का संचार कर, देशहित के कार्यों को नई दिशा और गति देनी होगी। 
 
जय हिंद। https://t.co/Uo21yPkTUK
Created at:  2022-08-15 06:39:53+00:00

Tweet text:  “To India, our much-loved motherland, the ancient, the eternal and the ever-new, we pay our reverent homage and we bind ourselves afresh to her service.”

Happy Independence Day! Jai Hind.

#IndiaAt75 https://t.co/J6slzNxJYo
Created at:  2022-08-15 02:27:46+00:00

Tweet text:  जालौर में निर्दयी शिक्षक द्वारा एक मासूम दलित बच्चे को बुरी तरह पीटे जाने के बाद उसकी मृत्यु की घटना बेहद दुःखद है। मैं इस क्रूर कृत्य की भर्त्सना करता हूं। पीड़ित परिवार के प्रति मेरी संवेदनाएं। 

आरोपी कठोर धाराओं के तहत गिरफ़्तार हो चुका है। उसे कड़ी से कड़ी सज़ा मिलनी चाहिए।
Created at:  2022-08-14 14:37:11+00:00

Tweet text:  “हाथरस पीड़िता का भाई परिवार का सदस्य नहीं है, इसलिए हम नौकरी और घर नहीं देंगे।”

आज 2 साल बाद भी पीड़िता के परिवार के साथ प्रताड़ना का सिलसिला जारी है। 

'बेटी बचाओ' की बात सिर्फ़ एक ढोंग थी, असल में भाजपा बेटियों के साथ अन्याय करने में कभी पीछे नहीं हटती। https://t.co/w3yNVUJz4Y
Created at:  2022-08-14 07:18:08+00:00

Tweet text:  “Development of the nation is intimately linked with understanding and application of science and technology by its people”: Dr. Vikram Sarabhai 

On his birth anniversary, humble tribute to the great visionary who helped foster scientific temper in India.
Created at:  2022-08-12 07:31:48+00:00

Tweet text:  भाई-बहन के पवित्र रिश्ते का सबसे ख़ूबसूरत दिन, आज देश भर में राखी का त्योहार धूम-धाम से मनाया जा रहा है। 

रक्षाबंधन के पावन पर्व पर सभी देशवासियों को मेरी हार्दिक शुभकामनाएं। मैं कामना करता हूं कि हर भाई-बहन के बीच का प्यार हमेशा बना रहे। https://t.co/D7G4BIQGLN
Created at:  2022-08-11 05:09:27+00:00
In [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing